package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.anno.PermissionName;
import cn.wolfcode.edu.domain.Employee;
import cn.wolfcode.edu.query.EmployeeQueryObject;

import cn.wolfcode.edu.service.IEmployeeService;
import cn.wolfcode.edu.util.JsonResult;
import cn.wolfcode.edu.util.PageResult;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * Created by abb on 2018-07-09.
 */
@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("")
    public String index(){
        return "employee";
    }
    @PermissionName("员工列表")
    @RequiresPermissions("employee:view")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(EmployeeQueryObject qo){
        return employeeService.query(qo);
    }

    /**
     * 返回所有employee的集合
     */
    @RequestMapping("listAll")
    @ResponseBody
    public List<Employee> listAll() {
        return employeeService.listAll();
    }

    @RequestMapping("listEmployee")
    @ResponseBody
    public List<Employee> listEmployee(){
        return employeeService.selectAll();
    }
    @RequestMapping("selectByEmployeeId")
    @ResponseBody
    public Employee selectByEmployeeId(Long id){
        return employeeService.selectByPrimaryKey(id);
    }
    @PermissionName("员工保存")
    @RequiresPermissions("employee:save")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Employee employee){
        JsonResult result = new JsonResult();
        try{
            employeeService.insert(employee);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
    @PermissionName("员工更新")
    @RequiresPermissions("employee:update")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Employee employee){
        JsonResult result = new JsonResult();
        try{
            employeeService.updateByPrimaryKey(employee);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }
    @PermissionName("员工删除")
    @RequiresPermissions("employee:delete")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try{
            employeeService.deleteToClose(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }

    //导出表格

    @RequestMapping("exportEmp")
    @ResponseBody
    @PermissionName("员工列表导出")
    @RequiresPermissions("employee:exportEmp") //用来表示权限的名字
    public void exportEmp(EmployeeQueryObject qo, HttpServletResponse response) throws Exception {
        //1.查询出列表数据
        qo.setRows(10000);
        List emps = employeeService.queryForList(qo);

        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标是 outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //2.处理标题
        //创建一个可以写 sheet ,
        WritableSheet sheet = workbook.createSheet("employee", 0);

        //创建一个单元格
        Label usernameLabel = new Label(0, 0, "用户名");
        Label realNameLabel = new Label(1, 0, "姓名");
        Label telLabel = new Label(2, 0, "电话");
        Label emailLabel = new Label(3, 0, "邮箱");
        Label deptLabel = new Label(4, 0, "部门");
        Label inputTimeLabel = new Label(5, 0, "录入时间");
        Label stateLabel = new Label(6, 0, "状态");
        Label adminLabel = new Label(7, 0, "管理员");

        sheet.addCell(usernameLabel);
        sheet.addCell(realNameLabel);
        sheet.addCell(telLabel);
        sheet.addCell(emailLabel);
        sheet.addCell(deptLabel);
        sheet.addCell(inputTimeLabel);
        sheet.addCell(stateLabel);
        sheet.addCell(adminLabel);


        //3.循环迭代list,每一个employee 对应表格中的一个行数据,二每一个employe的每一个字段,应该对应一行的某一个单元格
        for(int i = 0; i < emps.size() ;i++){
            Employee emp  = (Employee) emps.get(i);

            int row = i+1;

            Label tempusernameLabel = new Label(0, row, emp.getUsername());
            Label temprealNameLabel = new Label(1, row, emp.getRealName());
            Label temptelLabel = new Label(2, row, emp.getTel());
            Label tempemailLabel = new Label(3, row, emp.getEmail());
            Label tempdeptLabel = new Label(4, row, emp.getDepartment().getName());
            Label tempinputTimeLabel = new Label(5, row, emp.getInputTime().toLocaleString());

            String stateString = emp.getStatus()==Employee.NORMAL?"正常":"离职";
            Label tempstateLabel = new Label(6, row, stateString);

            String adminString = emp.isAdmin()?"是":"否";
            Label tempadminLabel = new Label(7, row, adminString);


            sheet.addCell(tempusernameLabel);
            sheet.addCell(temprealNameLabel);
            sheet.addCell(temptelLabel);
            sheet.addCell(tempemailLabel);
            sheet.addCell(tempdeptLabel);
            sheet.addCell(tempinputTimeLabel);
            sheet.addCell(tempstateLabel);
            sheet.addCell(tempadminLabel);

        }

        //4.下文件
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();

    }

 /* @PermissionName("员工")
  @RequiresPermissions("employee:hello")
    @RequestMapping("hello")
    @ResponseBody
    public JsonResult hello(Long id){
        JsonResult result = new JsonResult();
        try{
            employeeService.deleteToClose(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("有错误");
        }
        return result;
    }*/




    //查询移交历史的下拉框的角色 陈遵汉 专有资源 勿删
    @RequestMapping("selectEmployeeByHan")
    @ResponseBody
    public List<Employee> selectEmployeeByHan(){
        List<Employee> employees = employeeService.selectEmployeeByHan();
        return employees;
    }

    //列出所有的班主任
    @RequestMapping("listAllEmployeeBySn")
    @ResponseBody
    public List<Employee> listAllHomeroomTeacher(String sn){
        List<Employee> employees = employeeService.listAllEmployeeBySn(sn);
        return employees;
    }
    //查询移交历史的下拉框的角色 陈遵汉 专有资源 勿删

}
